package com.tduck.cloud.security.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.common.util.SecurityUtils;
import com.tduck.cloud.system.constant.SysConfigConstants;
import com.tduck.cloud.system.constant.SystemConstants;
import com.tduck.cloud.system.constant.UserConstants;
import com.tduck.cloud.system.entity.SysUserEntity;
import com.tduck.cloud.system.request.RegisterRequest;
import com.tduck.cloud.system.service.SysConfigService;
import com.tduck.cloud.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import static com.tduck.cloud.system.constant.SysConfigConstants.REGISTER_ROLE;

/**
 * 注册
 *
 * @author tduck
 */
@Component
@RequiredArgsConstructor
public class SysRegisterService {
    private final SysUserService userService;

    private final SysConfigService configService;

    /**
     * 注册
     */
    public Result<Long> register(RegisterRequest request) {
        if (!SystemConstants.YES_STATUS.equalsIgnoreCase(configService.getConfigValueByKey(SysConfigConstants.REGISTER_ENABLE))) {
            return Result.failed("暂未开启注册功能，请联系管理员！");
        }
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(request.getUserName()))) {
            return Result.failed("保存账号'" + request.getUserName() + "'失败，注册账号已存在");
        }
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setUserName(request.getUserName());
        if (StrUtil.isBlank(request.getNickName())) {
            request.setNickName(request.getUserName());
        }
        sysUser.setNickName(request.getNickName());
        sysUser.setEmail(request.getEmail());
        sysUser.setPhonenumber(request.getPhonenumber());
        sysUser.setSex(request.getSex());
        //默认注册用户角色
        String roleId = configService.getConfigValueByKey(REGISTER_ROLE);
        if (StrUtil.isNotEmpty(roleId) && NumberUtil.isNumber(roleId)) {
            sysUser.setRoleIds(CollUtil.newArrayList(Long.parseLong(roleId)));
        }
        //默认注册部门
        String deptId = configService.getConfigValueByKey(SysConfigConstants.REGISTER_DEPT);
        if (StrUtil.isNotEmpty(deptId) && NumberUtil.isNumber(deptId)) {
            sysUser.setDeptId(Long.parseLong(deptId));
        }
        // 默认密码
        String defaultPassword = configService.getConfigValueByKey(SysConfigConstants.DEFAULT_PASSWORD);
        if (StrUtil.isEmpty(request.getPassword())) {
            sysUser.setPassword(SecurityUtils.encryptPassword(StrUtil.emptyToDefault(defaultPassword, "123456")));
        } else {
            sysUser.setPassword(SecurityUtils.encryptPassword(request.getPassword()));
        }
        Long userId = userService.insertUser(sysUser);
        if (ObjectUtil.isNull(userId)) {
            return Result.failed("注册失败,请联系系统管理员！");
        }
        return Result.success(userId);
    }

}
